﻿module game.battle.background;

import std.random;

import gamelib.types;
import gamelib.graphics.renderer2d.renderer;
import gamelib.graphics.renderer2d.textureview;

import game.units;

class Background
{
private:
    static immutable Step = 150;
    immutable Rect mViewport;
    immutable Point[] mOrigPoints;
    Point[] mPoints;
public:
    this(in Rect viewport)
    {
        mViewport = viewport;
        Rect wideViewport = {x: mViewport.x - mViewport.w / 2,
                             y: mViewport.y - mViewport.h / 2,
                             w: mViewport.w * 2,
                             h: mViewport.h * 2};
        for(auto i = wideViewport.x; i <= wideViewport.x + wideViewport.w; i += Step)
        {
            for(auto j = wideViewport.y; j <= wideViewport.y + wideViewport.h; j += Step)
            {
                mPoints ~= Point(i, j);
            }
        }
        mOrigPoints = mPoints.idup;
    }

    void draw(Renderer ren, in Point center, in pos_t pos, in rot_t rotation)
    {
        ren.drawColor = Color(0,0,0);
        ren.clear();
        ren.drawColor = Color(255,255,255);

        assert(mPoints.length == mOrigPoints.length);
        foreach(i,ref p;mPoints)
        {
            Point pt = mOrigPoints[i];
            pt.x -= center.x;
            pt.y -= center.y;
            pt.x += cast(int)pos.y % Step;
            pt.y += cast(int)pos.x % Step;
            auto x = pt.x * rotation.x - pt.y * rotation.y + center.x;
            auto y = pt.x * rotation.y + pt.y * rotation.x + center.y;
            p.x = cast(int)x;
            p.y = cast(int)y;
        }

        ren.drawPoints(mPoints);
    }
}

